'''
Created on 2010-12-21

@author: lbh
'''

from abc import ABCMeta

class Object(object):
    '''root class in the hierarchy
    
    how to implements interfaces, here is an example:
    
    class MyIntf1(Interface):
    
        @abstractmethod
        def intf1_method(self): pass

        @abstractmethod
        def common_name_method(self): pass

    class MyIntf2(Interface):
    
        @abstractmethod
        def intf2_method(self): pass
        
         @abstractmethod
        def common_name_method(self): pass

    class MyObject(Object):

        class MyIntf1Impl(Interface):
            def intf1_method(self): print "MyIntf1.intf1_method"
            def common_name_method(self): print "MyIntf1.common_name_method"
        
        class MyIntf2Impl(Interface):
            def intf2_method(self): print "MyIntf2.intf_method"
            def common_name_method(self): print "MyIntf2.common_name_method"
         
        def __init__(self):
            super(MyObject, self).__init__()
            self.implements(self, MyIntf1, MyIntf1Impl)
            self.implements(self, MyIntf2, MyIntf2Impl)
    '''

    def __init__(self):
        '''Constructor
        '''
        self.__interfaces = {}
        
    def implements(self, intf_class, impl_class):
        self.__interfaces[intf_class] = impl_class(self)
        
    def supports(self, intf_class):
        return self.__interfaces.has_key(intf_class)
        
    def query_interface(self, intf_class):
        if self.supports(intf_class):
            return self.__interfaces[intf_class]
        else:
            return None
        
class Interface(object):
    
    __metaclass__ = ABCMeta
    
    def __init__(self, owner=None):
        self.__owner = owner
        
    @property
    def owner(self): return self.__owner
        

